/**
 * 
 */
package com.qachecklist.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qachecklist.domain.Item;
import com.qachecklist.domain.ItemDescription;

/**
 * @author hirokiterashima
 *
 */
public class ItemController extends DomainController {

	private static final long serialVersionUID = 1L;
	
	protected String homePath = "item";
	
	protected String attributeName = "item";
	
	protected String listAttributeName = "items";
	
	protected static Class<Item> getObjectClass() {
		return Item.class;
	}
	
	protected String getHomePath() {
		return homePath;
	}
	
	@Override
	protected String getObjectAttributeName() {
		return attributeName;
	}

	@Override
	protected String getObjectListAttributeName() {
		return listAttributeName;
	}
	
	protected Item getObjectById(Long id) {
		return (Item) Item.getById(id, Item.class);
	}
	
	protected List<Item> getObjectList() {
		return (List<Item>) Item.getList(Item.class);
	}

	protected void doPost(
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action == null || action.equals("create")) {
			Item domain = new Item();
			ItemDescription itemDescription = new ItemDescription();
			itemDescription.setDescription(request.getParameter("description"));
			domain.setItemDescription(itemDescription);
			domain.setName(request.getParameter("name"));
			domain.saveOrUpdate();
			response.sendRedirect(homePath);
		} else {
			String id = request.getParameter("id");
			Item domain = (Item) Item.getById(new Long(id), Item.class);
			ItemDescription itemDescription = new ItemDescription();
			itemDescription.setDescription(request.getParameter("description"));
			domain.setItemDescription(itemDescription);
			domain.setName(request.getParameter("name"));
			domain.saveOrUpdate();
			response.sendRedirect(homePath);
		}
    }
}
